package cn.jy.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.jy.common.user.ExpressUser;
import cn.jy.user.service.IExpressUserService;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;


@Slf4j
@RestController
@RequestMapping("/user")
public class ExpressUserController extends BaseController {
    @Autowired
    private IExpressUserService expressUserService;

    /**
     * 查询后台管理用户列表
     */
//    @RequiresPermissions("system:user:list")
    @GetMapping("/list")
    public TableDataInfo list(ExpressUser expressUser)
    {
        startPage();
        List<ExpressUser> list = expressUserService.selectExpressUserList(expressUser);
        return getDataTable(list);
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody ExpressUser expressUser)
    {
        ExpressUser user = expressUserService.login(expressUser);

        log.info("用户登录：{}",user);
        if (user != null){
            return success(user);
        }
        return error();
    }

    /**
     * 导出后台管理用户列表
     */
//    @RequiresPermissions("system:user:export")
    @Log(title = "后台管理用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExpressUser expressUser)
    {
        List<ExpressUser> list = expressUserService.selectExpressUserList(expressUser);
        ExcelUtil<ExpressUser> util = new ExcelUtil<>(ExpressUser.class);
        util.exportExcel(response, list, "后台管理用户数据");
    }

    /**
     * 获取后台管理用户详细信息
     */
//    @RequiresPermissions("system:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(expressUserService.selectExpressUserById(id));
    }

    /**
     * 新增后台管理用户
     */
//    @RequiresPermissions("system:user:add")
    @Log(title = "后台管理用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExpressUser expressUser)
    {
        return toAjax(expressUserService.insertExpressUser(expressUser));
    }

    /**
     * 修改后台管理用户
     */
//    @RequiresPermissions("system:user:edit")
    @Log(title = "后台管理用户", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ExpressUser expressUser)
    {
        return toAjax(expressUserService.updateExpressUser(expressUser));
    }

    /**
     * 删除后台管理用户
     */
//    @RequiresPermissions("system:user:remove")
    @Log(title = "后台管理用户", businessType = BusinessType.DELETE)
    @GetMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(expressUserService.deleteExpressUserByIds(ids));
    }

    @Log(title = "后台管理用户", businessType = BusinessType.UPDATE)
    @GetMapping("/status")
    public AjaxResult status(ExpressUser expressUser)
    {
        return toAjax(expressUserService.updateStatus(expressUser));
    }
}
